package net.huashitong.zhApp.controller;

import com.sirdc.modules.utils.ObjectUtils;
import net.huashitong.Project.entity.NewProjects;
import net.huashitong.Project.filter.NewProjectsFilter;
import net.huashitong.Project.service.MyProjectsService;
import net.huashitong.node.entity.NodeNewschedules;
import net.huashitong.node.service.NodeNewschedulesService;
import net.huashitong.oversee.service.CoordinateService;
import net.huashitong.user.databean.BnpUserDatabean;
import net.huashitong.user.entity.BnpUser;
import net.huashitong.zhApp.service.AppProjectsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/zhApp/myProjects")
public class AppMyProjectsController extends BaseCtrl {

    @Autowired
    private MyProjectsService myProjectsService;
    @Lazy
    @Autowired
    private AppProjectsService appProjectsService;
    @Autowired
    private NodeNewschedulesService nodeNewschedulesService;
    @Autowired
    private CoordinateService coordinateService;


    /**
     * 我的项目(无分页)
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/noPageLists",method = RequestMethod.POST)
    public Map<String, Object> queryNoPageLists(@RequestBody NewProjectsFilter filter) {

        BnpUser user = queryUserAuth(filter.getUserPhone());
        if(ObjectUtils.isBlank(user)){
            return coverMessage("202", "无用户权限");
        }
        List<NewProjects> resList = appProjectsService.queryProjectsNoPageByUser(user, filter);

        Map<String, Object> result = new HashMap<>();
        result.put("code", "200");
        result.put("message", "成功获取数据");
        result.put("userAuth", user);
        result.put("data", resList);
        return result;
    }


    /**
     * 根据项目Id查询确认需要进行中的节点(无分页)
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/noPageNodeLists",method = RequestMethod.POST)
    public Map<String, Object> queryNodeNoPageListsByProjectId(@RequestBody NewProjectsFilter filter) {

        //查询确定的进行中的节点
        List<NodeNewschedules> nodeNewschedules = nodeNewschedulesService.getNodeStateAndStatusByProjectId(filter.getProjectId());
        List<NodeNewschedules> data = appProjectsService.dealWithNodes(nodeNewschedules);
        return coverMessage("200", "成功获取数据", data);

    }


    /**
     * 获取涉及部门
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryDeptList",method = RequestMethod.GET)
    public Map<String, Object> queryDeptList() {
        //涉及单位（involveUnit）
        List<BnpUserDatabean> deptLists = coordinateService.queryAllInvolveUnit();
        return coverMessage("200", "成功获取数据", deptLists);
    }


    /**
     * 项目统计汇总(APP端)
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/statisticsSummary/{phone}", method = RequestMethod.GET)
    public Map<String, Object> planeOpenProject(@PathVariable String phone) {
        BnpUser user = queryUserAuth(phone);
        if(ObjectUtils.isBlank(user)){
            return coverMessage("202", "无用户权限");
        }
        Map<String,Object> data = myProjectsService.queryStatisticsSummary(user);

        Map<String, Object> result = new HashMap<>();
        result.put("code", "200");
        result.put("message", "成功获取数据");
        result.put("userAuth", user);
        result.put("data", data);
        return result;
    }
}
